import { Component, OnInit, Input, SimpleChanges } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { ProjectService } from 'src/app/core/service/project.service';
import { ApplicationService } from 'src/app/core/service/application.service';
import { markDirty } from '@angular/core/src/render3';

@Component({
  selector: 'app-overview',
  templateUrl: './overview.component.html',
  styleUrls: ['./overview.component.scss']
})
export class OverviewComponent implements OnInit {
  @Input() title = '';
  @Input() projectUuid = '';
  @Input() appUuid = '';
  @Input() refreshAppOverview;
  @Input() refreshProOverview;
  @Input() refreshMark;

  appNum = 0;
  projectNum = 0;
  cveNum = 0;
  licenseNum = 0;
  jarNum = 0;

  constructor(
    private router: Router,
    private projectService: ProjectService,
    private applicationService: ApplicationService,
    private activeRoute: ActivatedRoute
  ) { }

  /**
   * 调到项目列表
   */
  goListPage(): void {
    // this.router.navigateByUrl('/layout/project/project-list');
  }

  /**
   * 跳到组件列表页
   */
  goJarPage(): void {
    if (this.projectUuid) {
      // 从项目详情 跳到组件列表
      this.router.navigateByUrl('/layout/project/jar-list?projectUuid=' + this.projectUuid);
    } else {
      // 从项目的dashboard/项目列表页 跳到组件列表，应该是所有的组件数
      this.router.navigateByUrl('/layout/project/jar-list');
    }
  }

  goAppPage() {
    if (this.projectUuid) {
      this.router.navigate(['/layout/application/app-list'], { queryParams: { 'projectUuid': this.projectUuid, 'title': '应用' } });
    }
  }

  goCvePage(): void {
    if (this.projectUuid) {
      this.router.navigateByUrl('/layout/project/cve-list?projectUuid=' + this.projectUuid);
    } else {
      this.router.navigateByUrl('/layout/project/cve-list');
    }
  }

  goLicensePage(): void {
    if (this.projectUuid) {
      this.router.navigateByUrl('/layout/project/license-list?projectUuid=' + this.projectUuid);
    } else {
      this.router.navigateByUrl('/layout/project/license-list');
    }
  }

  ngOnChanges(changes: SimpleChanges) {

    console.log('test===>', this.refreshMark);
    console.log('test1===>', this.refreshAppOverview);
    console.log('test2===>', this.refreshProOverview);

    if (this.refreshMark === 'app') {
      this.appNum = this.refreshAppOverview.appCount;
      this.jarNum = this.refreshAppOverview.jarCount;
      this.cveNum = this.refreshAppOverview.cveCount;
      this.licenseNum = this.refreshAppOverview.licenseCount;
    } else if (this.refreshMark === 'pro') {
      this.projectNum = this.refreshProOverview.projectNum;
      this.jarNum = this.refreshProOverview.jarNum;
      this.cveNum = this.refreshProOverview.cveNum;
      this.licenseNum = this.refreshProOverview.licenseNum;
    }

  }

  ngOnInit() {
    if ((this.title === '应用' && this.projectUuid) || this.title !== '应用') {
      this.projectCount(this.projectUuid);
    } else if (this.title === '应用' && this.appUuid) {
      this.appCount(this.projectUuid, this.appUuid);
    } else {
      this.appCount(this.projectUuid);
    }
  }

  /**
   * 显示项目相关数据
   */
  projectCount(projectUuid: string) {
    console.log('=== Janine.projectCount ===> 进入');

    this.projectService.projectCount(projectUuid)
      .subscribe((countData) => {
        if (countData.status) {
          this.appNum = countData.data.appNum;
          this.projectNum = countData.data.projectNum;
          this.jarNum = countData.data.jarNum;
          this.cveNum = countData.data.cveNum;
          this.licenseNum = countData.data.licenseNum;
        }
      });
  }

  /**
   * 显示应用相关数据
   */
  appCount(projectUuid?: string, appUuid?: string) {
    console.log('=== Janine.appCount ===> 进入');
    this.applicationService.appCount(projectUuid, appUuid)
      .subscribe((appData) => {
        if (appData.status) {
          this.appNum = appData.data.appCount;
          this.jarNum = appData.data.jarCount;
          this.cveNum = appData.data.cveCount;
          this.licenseNum = appData.data.licenseCount;
        }
      });
  }

}
